package com.cndw.kungfu.business.gm;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.comm.AccessInspection;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.UtilsOfBusiness;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.business.IConstantsConfigManger;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 禁言，封号，封ip
 * 
 */
@Controller
@SuppressWarnings("unchecked")
public class BusinessRestrict {

	@Autowired
	private IConstantsConfigManger configManger;

	/**
	 * 增加指导员
	 * 
	 * @param session
	 * @param param
	 * @param endTime
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage addInstructor(Session session, String sign, int playerType, String players, String endTime) {
		Set<Long> playerIds = (Set<Long>) getValue(ConstantsConfig.SYS_INSTRUCTOR);
		if (playerIds == null) {
			synchronized (ConstantsConfig.class) {
				if (playerIds == null) {
					return ResponseMessage.getSuccMessage(true);
				}
			}
		}

		Set<Long> addPlayers = new HashSet<Long>();
		for (String playerValue : players.replace("\r", "").split("\n")) {
			Player player = UtilsOfBusiness.getPlayer(playerType, playerValue);
			if (!BlankUtils.isBlank(player)) {
				playerIds.add(player.getPlayerId());
				addPlayers.add(player.getPlayerId());
			}
		}

		if (!setValue(ConstantsConfig.SYS_INSTRUCTOR, ConstantsConfig.format(playerIds))) {
			addPlayers.clear();
		}
		
		List<Object[]> playerList = new ArrayList<Object[]>();
		for (Long playerId : playerIds) {
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			playerList.add(new Object[] { player.getPlayerId(), player.getAccountName(), player.getPlayerName() });
		}
		return ResponseMessage.getSuccMessage(playerList);
	}

	/**
	 * 删除指导员
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage deleteInstructor(Session session, String sign, int playerType, String players) {
		Set<Long> playerIds = (Set<Long>) getValue(ConstantsConfig.SYS_INSTRUCTOR);
		if (playerIds == null) {
			synchronized (ConstantsConfig.class) {
				if (playerIds == null) {
					return ResponseMessage.getSuccMessage(true);
				}
			}
		}

		List<Long> removePlayers = new ArrayList<Long>();
		for (String playerValue : players.replace("\r", "").split("\n")) {
			Player player = UtilsOfBusiness.getPlayer(playerType, playerValue);
			if (!BlankUtils.isBlank(player)) {
				playerIds.remove(player.getPlayerId());
				removePlayers.add(player.getPlayerId());
			}
		}

		if (!setValue(ConstantsConfig.SYS_INSTRUCTOR, ConstantsConfig.format(playerIds))) {
			removePlayers.clear();
		}

		List<Object[]> playerList = new ArrayList<Object[]>();
		for (Long playerId : playerIds) {
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			playerList.add(new Object[] { player.getPlayerId(), player.getAccountName(), player.getPlayerName() });
		}
		return ResponseMessage.getSuccMessage(playerList);
	}

	/**
	 * 获取指导员列表
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage getInstructors(Session session, String sign) {
		Set<Long> playerIds = (Set<Long>) getValue(ConstantsConfig.SYS_INSTRUCTOR);
		if (!BlankUtils.isBlank(playerIds)) {
			List<Object[]> players = new ArrayList<Object[]>();
			for (Long playerId : playerIds) {
				Player player = Context.getPlayerServer().getByPlayerId(playerId);
				players.add(new Object[] { player.getPlayerId(), player.getAccountName(), player.getPlayerName() });
			}
			return ResponseMessage.getSuccMessage(players);
		}
		return ResponseMessage.getSuccMessage(null);
	}

	/**
	 * 禁言
	 * 
	 * @param session
	 * @param param
	 * @param endTime
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage setChat(Session session, String sign, int playerType, String players, String endTime) {
		Map<Long, Integer> playerMap = (Map<Long, Integer>) getValue(ConstantsConfig.SYS_LOCK_CHAT);
		if (playerMap == null) {
			synchronized (ConstantsConfig.class) {
				if (playerMap == null) {
					playerMap = new HashMap<Long, Integer>();
				}
			}
		}

		Integer time = Integer.valueOf(endTime);
		Map<Long, Integer> addPlayers = new HashMap<Long, Integer>();
		for (String playerValue : players.replace("\r", "").split("\n")) {
			Player player = UtilsOfBusiness.getPlayer(playerType, playerValue);
			if (!BlankUtils.isBlank(player)) {
				playerMap.put(player.getPlayerId(), time);
				addPlayers.put(player.getPlayerId(), time);
			}
		}

		if (!setValue(ConstantsConfig.SYS_LOCK_CHAT, ConstantsConfig.format(playerMap))) {
			addPlayers.clear();
		}

		return ResponseMessage.getSuccMessage(ConstantsConfig.parse(addPlayers, ConstantsConfig.SYS_LOCK_CHAT));
	}

	/**
	 * 解除禁言
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage deleteChat(Session session, String sign, int playerType, String players) {
		Map<Long, Integer> playerMap = (Map<Long, Integer>) getValue(ConstantsConfig.SYS_LOCK_CHAT);
		if (playerMap == null) {
			synchronized (ConstantsConfig.class) {
				if (playerMap == null) {
					return ResponseMessage.getSuccMessage(true);
				}
			}
		}

		List<Long> removePlayers = new ArrayList<Long>();
		for (String playerValue : players.replace("\r", "").split("\n")) {
			Player player = UtilsOfBusiness.getPlayer(playerType, playerValue);
			if (!BlankUtils.isBlank(player)) {
				playerMap.remove(player.getPlayerId());
				removePlayers.add(player.getPlayerId());
			}
		}

		if (!setValue(ConstantsConfig.SYS_LOCK_CHAT, ConstantsConfig.format(playerMap))) {
			removePlayers.clear();
		}

		return ResponseMessage.getSuccMessage(removePlayers);
	}

	/**
	 * 获取禁言列表
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage getChats(Session session, String sign) {
		Map<Long, Integer> players = (Map<Long, Integer>) getValue(ConstantsConfig.SYS_LOCK_CHAT);
		if (!BlankUtils.isBlank(players)) {
			return ResponseMessage.getSuccMessage(ConstantsConfig.parse(players, ConstantsConfig.SYS_LOCK_CHAT));
		}
		return ResponseMessage.getSuccMessage(null);
	}

	/**
	 * 封号
	 * 
	 * @param session
	 * @param param
	 * @param endTime
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage setPlayer(Session session, String sign, int playerType, String players, String endTime) {
		Map<Long, Integer> playerMap = (Map<Long, Integer>) getValue(ConstantsConfig.SYS_LOCK_PLAYERID);
		if (playerMap == null) {
			synchronized (ConstantsConfig.class) {
				if (playerMap == null) {
					playerMap = new HashMap<Long, Integer>();
				}
			}
		}

		Integer time = Integer.valueOf(endTime);
		Map<Long, Integer> addPlayers = new HashMap<Long, Integer>();
		for (String playerValue : players.replace("\r", "").split("\n")) {
			Player player = UtilsOfBusiness.getPlayer(playerType, playerValue);
			if (!BlankUtils.isBlank(player)) {
				playerMap.put(player.getPlayerId(), time);
				addPlayers.put(player.getPlayerId(), time);
			}
		}

		if (!setValue(ConstantsConfig.SYS_LOCK_PLAYERID, ConstantsConfig.format(playerMap))) {
			addPlayers.clear();
		}

		return ResponseMessage.getSuccMessage(ConstantsConfig.parse(addPlayers, ConstantsConfig.SYS_LOCK_PLAYERID));
	}

	/**
	 * 解除封号
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage deletePlayer(Session session, String sign, int playerType, String players) {
		Map<Long, Integer> playerMap = (Map<Long, Integer>) getValue(ConstantsConfig.SYS_LOCK_PLAYERID);
		if (playerMap == null) {
			synchronized (ConstantsConfig.class) {
				if (playerMap == null) {
					return ResponseMessage.getSuccMessage(true);
				}
			}
		}

		List<Long> removePlayers = new ArrayList<Long>();
		for (String playerValue : players.replace("\r", "").split("\n")) {
			Player player = UtilsOfBusiness.getPlayer(playerType, playerValue);
			if (!BlankUtils.isBlank(player)) {
				playerMap.remove(player.getPlayerId());
				removePlayers.add(player.getPlayerId());
			}
		}

		if (!setValue(ConstantsConfig.SYS_LOCK_PLAYERID, ConstantsConfig.format(playerMap))) {
			removePlayers.clear();
		}

		return ResponseMessage.getSuccMessage(removePlayers);
	}

	/**
	 * 获取封号列表
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage getPlayers(Session session, String sign) {
		Map<Long, Integer> players = (Map<Long, Integer>) getValue(ConstantsConfig.SYS_LOCK_PLAYERID);
		if (!BlankUtils.isBlank(players)) {
			return ResponseMessage.getSuccMessage(ConstantsConfig.parse(players, ConstantsConfig.SYS_LOCK_CHAT));
		}
		return ResponseMessage.getSuccMessage(null);
	}

	/**
	 * 设置要过滤的IP
	 * 
	 * @param session
	 * @param param
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage filterIp(Session session, String sign, String param) {
		boolean result = setValue(ConstantsConfig.SYS_LOCK_IP, param);
		if (result) {
			AccessInspection.reloadLockIp();
		}
		return ResponseMessage.getSuccMessage(result);
	}

	/**
	 * 获取ip过滤列表
	 * 
	 * @param session
	 * @return
	 */
	@UnAuth
	@GmCommand
	public ResponseMessage getFilterIp(Session session, String sign) {
		return ResponseMessage.getSuccMessage(getValue(ConstantsConfig.SYS_LOCK_IP));
	}

	private boolean setValue(String key, String value) {
		try {
			Field field = ConstantsConfig.class.getDeclaredField(key);
			ConstantsConfig.setValue(field, value);
			return configManger.updateConfig(key, value);
		} catch (Exception e) {
			return false;
		}
	}

	private Object getValue(String key) {
		try {
			Field field = ConstantsConfig.class.getDeclaredField(key);
			field.setAccessible(true);
			return field.get(null);
		} catch (Exception e) {
			return null;
		}
	}
}
